package com.shiguiwu.springmybatis.javabase.collect;

import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: Stream 流的测试
 * @author: stone
 * @date: Created by 2022/9/1 23:37
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.collect
 */
@Slf4j
public class StreamTests {


    public static void main(String[] args) {
        testNull();
    }


    /**
     * 测试元素是 null 的情况,或者是集合是 null
     * 会爆空指针异常、
     *
     * 如果集合是空，就是没有元素
     * 直接返回，不会爆空指针
     *
     */
    public static void testNull() {
        List<Person> list = new ArrayList<>();
        list.add(null);
        Person p = new Person();
        p.setName("小小");
        list.add(p);
        Person p1 = new Person();
        p1.setName("小猪佩奇");
        list.add(p1);
        list.add(null);

        List<String> collect = list.stream().map(Person::getName).collect(Collectors.toList());

        log.info("测试 null元素 ===>  {}", JSONUtil.toJsonStr(collect));

    }



    @Data
    private static class Person {
        private String name;

        private int age;

        private Double weight;

        private BigDecimal way;

        private long height;


    }



}
